#include "NxServiceAccess.h"
#include "NxModelService.h"
#include "NxModelServiceFactory.h"
#include "NxDataFactory.h"
#include "NxModelServiceRecordFactory.h"
#include "NxModelServiceInstance.h"
#include "NxModelServiceInstanceFactory.h"
#include "../utils/NxHttp.h"

int NJGIS::SERVICE::NjServiceAccess::getModelServicesList( std::vector<NJGIS::SERVICE::INjModelService*> &list )
{
	std::string url;
	this->getBaseUrl(url);
	url = url + "modelser/json/all";
	Json::Value jResonese;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResonese);

	if(jResonese["result"].asString() == "suc")
	{
		Json::Value jMss = jResonese["data"];
		int count = jMss.size();
		for(int index = 0; index < count; index++)
		{
			list.push_back(NjModelServiceFactory::createModelServiceByJSON(jMss[index], this->getIP(), this->getPort()));
		}
	}
	else
	{
		return -1;
	}
	return 1;
}

NJGIS::SERVICE::INjModelService* NJGIS::SERVICE::NjServiceAccess::getModelServiceByOID( const char* oid )
{
	std::string url;
	this->getBaseUrl(url);
	url = url + "modelser/json/" + oid;
	Json::Value jResonese;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResonese);

	if(jResonese["result"].asString() == "suc")
	{
		Json::Value jMs = jResonese["data"];
		return NjModelServiceFactory::createModelServiceByJSON(jMs, this->getIP(), this->getPort());
	}
	else
	{
		return NULL;
	}
}

int NJGIS::SERVICE::NjServiceAccess::getModelServicesByName( const char* name, std::vector<NJGIS::SERVICE::INjService*> &list )
{
	//! TODO DX

	//waite for later
	return NULL;
}

NJGIS::SERVICE::INjData* NJGIS::SERVICE::NjServiceAccess::getDataServiceByID( const char* id )
{
	std::string url;
	this->getBaseUrl(url);
	url = url + "geodata/json/" + id;
	Json::Value jResonese;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResonese);


	if(jResonese["result"].asString() == "suc")
	{
		Json::Value jData = jResonese["data"];
		if(jData.type() == Json::stringValue && jData.asString() == "")
		{
			return NULL;
		}
		NJGIS::SERVICE::INjData* pData = NJGIS::SERVICE::NjDataFactory::createDataByJSON(jData, this->getIP(), this->getPort());
		return pData;
	}
	return NULL;
}

NJGIS::SERVICE::INjData* NJGIS::SERVICE::NjServiceAccess::uploadDataByFile( const char* filepath, const char* tag )
{
	//! TODO Check the file


	std::string url = "";
	this->getBaseUrl(url);
	url = url + "geodata?type=file";

	NJGIS::SERVICE::INjFormParam* pFormData = NJGIS::SERVICE::NjFormHelper::createForm();
	NJGIS::SERVICE::INjLocalFiles* pFiles = NJGIS::SERVICE::NjFileHelper::createLocalFiles();

	pFiles->insertFile("myfile", filepath);
	pFormData->insertParam("gd_tag", tag);

	Json::Value jResonese;
	int code = NJGIS::SERVICE::NjHttpHelper::request_post_withform_json_sync(url.c_str(), pFormData, pFiles, jResonese);
	if(code != 1) return NULL;
	if(jResonese["result"].asString() == "suc")
	{
		std::string gdid = jResonese["data"].asString();

		//! get the data information
		this->getBaseUrl(url);
		url = url + "geodata/json/" + gdid;
		Json::Value jGeoData;
		NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jGeoData);
		if (jGeoData["result"].asString() == "suc")
		{
			NJGIS::SERVICE::INjData* pData = NJGIS::SERVICE::NjDataFactory::createDataByJSON(jGeoData["data"], this->getIP(), this->getPort());
			return pData;
		}
	}
	return NULL;
}

NJGIS::SERVICE::INjData* NJGIS::SERVICE::NjServiceAccess::uploadDataStream( const char* stream, const char* tag )
{
	//! TODD upload data stream
	return NULL;
}

NJGIS::SERVICE::INjDataConfiguration* NJGIS::SERVICE::NjServiceAccess::createDataConfig()
{
	return new NJGIS::SERVICE::NjDataConfiguration();
}

NJGIS::SERVICE::INjModelServiceRecord* NJGIS::SERVICE::NjServiceAccess::getModelServiceRecordByID( const char* msrid )
{
	std::string url;
	this->getBaseUrl(url);
	url += "modelserrun/json/" + std::string(msrid);
	//NJGIS::SERVICE::INjModelServiceRecord* pRecord = NULL;

	Json::Value jResponse;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResponse);
	if(jResponse["result"].asString() == "suc")
	{
		Json::Value jMsr = jResponse["data"];
		return NJGIS::SERVICE::NjModelServiceRecordFactory::createModelServiceRecordByJSON(jMsr, this->getIP(), this->getPort());
	}else{
		return NULL;
	}
}

NJGIS::SERVICE::INjModelServiceInstance* NJGIS::SERVICE::NjServiceAccess::getModelServiceInstanceByGUID( const char* guid )
{
	INjModelServiceInstance* pInstance;

	std::string url;
	this->getBaseUrl(url);
	url += "modelins/json/" + std::string(guid);
	//NJGIS::SERVICE::INjModelServiceRecord* pRecord = NULL;

	Json::Value jResponse;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResponse);
	if(jResponse["result"].asString() == "suc")
	{
		Json::Value jMis = jResponse["data"];
		return NJGIS::SERVICE::NjModelServiceInstanceFactory::createModelServiceInstacnce(jMis, this->getIP(), this->getPort());
	}else{
		return NULL;
	}
}
